package com.ea.controller;

import java.io.IOException;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.*;

import javax.servlet.http.*;

@SuppressWarnings("serial")
public class Contact extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException{

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = req.getParameter("yourenquiry") + '\n' + req.getParameter("youremail");		
		
		try{
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("vegawar@gmail.com", req.getParameter("yourname"), "UTF-8"));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress("stephane_rainville@hotmail.com", "Web Site"));
            msg.setSubject("Message From Enterprise-Architect.ca");
            msg.setText(msgBody);
            Transport.send(msg);			

            RequestDispatcher rd = req.getRequestDispatcher("mailsent.html");
            rd.forward(req,resp);
		}
		catch(AddressException e){
			resp.getWriter().println(e.getMessage());
		}
		catch(MessagingException e){
			resp.getWriter().println(e.getMessage());			
		}
		catch(ServletException e){
			resp.getWriter().println(e.getMessage());	
		}
	}
	
}
